using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MyUnityLib.Tool;
using MyUnityLib.ClassExtension;

namespace ProductExpirationAssistant
{
    /// <summary>提示弹窗</summary>
    public class TipPopupPanel : MonoBehaviour
    {
        private Text m_Text;

        private Button m_YesBtn;
        private Button m_NoBtn;

        public void Init()
        {
            m_Text = transform.FindComponentByPath<Text>("Text");
            m_YesBtn = transform.FindComponentByPath<Button>("YesBtn");
            m_NoBtn = transform.FindComponentByPath<Button>("NoBtn");
            UITool.BindButtonEvent(new Button[] { m_YesBtn, m_NoBtn }, OnButton);

            Messenger.AddListener<Kind, string>(EventName.ShowTipPopup, Show);
        }
        private void OnDestroy()
        {
            Messenger.RemoveListener<Kind, string>(EventName.ShowTipPopup, Show);
        }

        private void Show(Kind kind, string text)
        {
            if (gameObject.activeSelf)
            {
                Debug.LogError("重复打开");
                return;
            }

            this.SetActive(true);
            m_YesBtn.SetActive(false);
            m_NoBtn.SetActive(false);

            switch (kind)
            {
                case Kind.Yes:
                    {
                        m_YesBtn.SetActive(true);
                        m_YesBtn.RectTransform().SetLocalX(0f);
                        break;
                    }
                case Kind.Yes_No:
                    {
                        m_YesBtn.SetActive(true);
                        m_YesBtn.RectTransform().SetLocalX(-150f);
                        m_NoBtn.SetActive(true);
                        m_NoBtn.RectTransform().SetLocalX(150f);
                        break;
                    }
            }

            m_Text.text = text;
        }

        private void OnButton(Button button)
        {
            ReplyKind kind = ReplyKind.Yes;
            switch (button.name)
            {
                case "YesBtn":
                    {
                        kind = ReplyKind.Yes;
                        break;
                    }
                case "NoBtn":
                    {
                        kind = ReplyKind.No;
                        break;
                    }
            }

            try
            {
                Messenger.Broadcast<ReplyKind>(EventName.CloseTipPopup, kind);
            }
            catch (System.Exception) { }

            this.SetActive(false);
        }

        /// <summary>提示弹窗类型</summary>
        public enum Kind
        {
            Yes,
            Yes_No,
        }

        /// <summary>回复类型</summary>
        public enum ReplyKind
        {
            Yes,
            No
        }
    }
}